
import sys
import time

import pyautogui
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget
from autoClickGui import Ui_Form

class autoClickApp(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.x = 0
        self.y = 0
        self.ui.pushButton_3.clicked.connect(self.exit)
        self.ui.pushButton_4.clicked.connect(self.start)
        self.ui.pushButton_5.clicked.connect(self.getAddress)
        QMessageBox.information(self, "欢迎使用", "请将浏览器放大指数设为100%（ctrl+滚轮），打开校友邦页面，拖拽本软件，露出'审批通过',第一位同学审批先点'自动设置'再点'开始'，后面的只点击'开始'，每次可批阅一个学生的20篇日志")
    def getApprovalAddress(self):
        # self.x, self.y = pyautogui.locateCenterOnScreen('审批通过.png')
        # print(self.x, self.y)
        try:
            self.Approval.x,self.Approval.y= pyautogui.locateCenterOnScreen('审批通过.png')
            print(self.Approval.x,self.Approval.y)
            self.exit_x, self.exit_y = pyautogui.locateCenterOnScreen('关闭.png')
            print( self.exit_x, self.exit_y)
            QMessageBox.information(self,"欢迎使用","自动设置成功")
        except BaseException as err:
            print(err)
    def getGoApprovalAddress(self):
        try:
            self.goApproval.x, self.goApproval.y = pyautogui.locateCenterOnScreen('去评阅.png')
            time.sleep(0.5)
            self.click_mouse((self.goApproval.x, self.goApproval.y), 1, 0.5)
        except BaseException as err:
            print(err)
    def getAddress(self):
        try:
            self.x, self.y = pyautogui.locateCenterOnScreen('审批通过.png')
            print(self.x, self.y)
            QMessageBox.information(self, "欢迎使用", "自动设置成功")
        except pyautogui.ImageNotFoundException:
            QMessageBox.information(self, "识别失败", "看下浏览器是不是100%缩进，再不行找老张")

    def click_mouse(self,position, click_count=1, interval=1):
        try:
            for i in range(click_count):
                pyautogui.click(x=position[0], y=position[1])
                print(f"已点击 {i + 1} 次")
                time.sleep(interval)
            print("点击结束")
        except Exception as e:
            print(f"点击失败: {e}")

    def start(self):
        try:
            if self.x == 0 or self.y == 0:
                QMessageBox.information(self, "设置不对", "设置不对，请先自动设置再开始，再不行找老张")
        # self.x, self.y = self.get_mouse_position()
            else:
                for i in range(0, 20):
                    time.sleep(1)
                    self.click_mouse((self.x, self.y), 1, 0.5)
                     

        except BaseException as err:
            print(err)
    def exit(self):
        sys.exit(app.exec_())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    autoClick = autoClickApp()
    autoClick.setWindowTitle("自动评价-made by 张鸿")
    autoClick.show()
    sys.exit(app.exec_())


